package me.nibo.tree.utils.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;

import java.util.List;

import me.nibo.tree.utils.Node;
import me.nibo.tree.utils.TreeHelper;

/**
 * @author nibo
 */
public abstract class TreeListViewAdapter<T> extends BaseAdapter {
    private List<Node> mAllNodes;
    private List<Node> mVisibleNodes;
    protected Context mContext;
    protected ListView mListView;
    protected LayoutInflater inflater;
    private OnTreeNodeClickListener onTreeNodeClickListener;

    public TreeListViewAdapter(Context context, List<T> datas, int defaultExpandLevel,
                               ListView listView)
            throws IllegalAccessException {
        mContext = context;
        inflater = LayoutInflater.from(context);
        mAllNodes = TreeHelper.getSortedNode(datas, defaultExpandLevel);
        mVisibleNodes = TreeHelper.filterVisibleNode(mAllNodes);
        mListView = listView;
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                expandOrCollapse(position);
                if (null != onTreeNodeClickListener) {
                    onTreeNodeClickListener.onNodeClick(mVisibleNodes.get(position), position);
                }
            }
        });
    }

    /**
     * 展开或收起
     *
     * @param position 位置索引
     */
    private void expandOrCollapse(int position) {
        Node node = mVisibleNodes.get(position);
        if (node.isLeaf()) {
            return;
        }
        node.setExpand(!node.isExpand());
        mVisibleNodes = TreeHelper.filterVisibleNode(mAllNodes);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mVisibleNodes.size();
    }

    @Override
    public Object getItem(int position) {
        return mVisibleNodes.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Node node = mVisibleNodes.get(position);
        convertView = getConvertView(node, position, convertView, parent);
        convertView.setPadding(node.getLevel() * 30, 3, 3, 3);
        return convertView;
    }

    public abstract View getConvertView(Node node, int position,
                                        View convertView, ViewGroup parent);

    public void setOnTreeNodeClickListener(OnTreeNodeClickListener onTreeNodeClickListener) {
        this.onTreeNodeClickListener = onTreeNodeClickListener;
    }

    public interface OnTreeNodeClickListener {
        void onNodeClick(Node node, int position);
    }
}
